package com.ly.controller;

import com.github.pagehelper.PageInfo;
import com.ly.enums.CodeEnum;
import com.ly.enums.TipEnum;
import com.ly.pojo.Student;
import com.ly.pojo.bo.StudentBO;
import com.ly.service.StudentService;
import com.ly.utils.DataGridData;
import com.ly.utils.JSONResult;
import com.ly.utils.ValidUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("student")
@Slf4j
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 增加
     * @param studentBO
     * @return
     */
    @PostMapping("addStudent")
    public JSONResult addStudent(@Valid @RequestBody StudentBO studentBO,
                                 BindingResult result) {
        //判断BindingResult是否有错误，错误信息会包含在里边，如果有则直接return
        if(result.hasErrors()) {
            Map<String, String> errors = ValidUtils.getErrors(result);
            return JSONResult.errorMap(errors);
        }
        JSONResult jsonResult = new JSONResult();
        try {
            studentService.saveStudent(studentBO);
            jsonResult.setMsg(TipEnum.ADDSUCCESSMSG.getName());
            jsonResult.setStatus(CodeEnum.SUCCCESSCODE.getCode());
        } catch (Exception e) {
            jsonResult.setMsg(TipEnum.ADDERRORMSG.getName());
            jsonResult.setStatus(CodeEnum.ERRORCODE.getCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    /**
     * 查询列表  分页
     * 自定义通用mapper内嵌查询
     */
    @GetMapping("queryByCondition")
    public DataGridData<Student> queryByCondition(Student student, int page, int pageSize){
        DataGridData<Student> data = new DataGridData<Student>();
        try {
            PageInfo<Student> pageInfo= studentService.queryByCondition(student, page, pageSize);
            data.setTotal(pageInfo.getTotal());
            data.setRows(pageInfo.getList());
            data.setMsg(TipEnum.QUERYSUCCESS.getName());
            data.setStatus(CodeEnum.SUCCCESSCODE.getCode());
        } catch (Exception e) {
            data.setMsg(TipEnum.QUERYFAIL.getName());
            data.setStatus(CodeEnum.ERRORCODE.getCode());
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 自定义SQL查询
     */
    @GetMapping("queryCustomList")
    public DataGridData<Student> queryCustomList(Student student, int page, int pageSize){
        DataGridData<Student> data = new DataGridData<Student>();
        try {
            PageInfo<Student> pageInfo= studentService.queryCustomList(student, page, pageSize);
            data.setTotal(pageInfo.getTotal());
            data.setRows(pageInfo.getList());
            data.setMsg(TipEnum.QUERYSUCCESS.getName());
            data.setStatus(CodeEnum.SUCCCESSCODE.getCode());
        } catch (Exception e) {
            data.setMsg(TipEnum.QUERYFAIL.getName());
            data.setStatus(CodeEnum.ERRORCODE.getCode());
            e.printStackTrace();
        }
        return data;
    }

    /*
    *   这里就不写  修改和删除操作了 后续有兴趣的可以自己写  我是懒得写了
    *
    * */





}
